package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.PaginationVO;
import com.itheima.entity.Advimg;
import com.itheima.mapper.FriendshipLinkMapper;
import com.itheima.service.FriendshipLinkService;
import com.itheima.vo.LinkVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class FriendshipLinkServiceImpl implements FriendshipLinkService {
    private final FriendshipLinkMapper friendshipLinkMapper;

    @Override
    public PaginationVO<LinkVO> page(String page, String limit, String type) {

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<LinkVO> link = friendshipLinkMapper.getPageContent(type);
        PageInfo<LinkVO> pageInfo = new PageInfo<>(link);
        long count = pageInfo.getTotal();
        int totalPages = pageInfo.getPages();
        int pageSize = pageInfo.getPageSize();
        int currentPage = pageInfo.getNextPage();
        List<LinkVO> linkList = pageInfo.getList();

        PaginationVO<LinkVO> paginationVO = new PaginationVO<>();
        paginationVO.setCount(count);
        paginationVO.setTotalPages(totalPages);
        paginationVO.setPageSize(pageSize);
        paginationVO.setCurrentPage(currentPage);
        paginationVO.setData(linkList);
        return paginationVO;
    }

    @Override
    public void addLink(LinkVO linkVO) {
        linkVO.setDefault_data("1");
        friendshipLinkMapper.addLink(linkVO);
    }

    @Override
    public void updateLink(LinkVO linkVO) {
        friendshipLinkMapper.updateLink(linkVO);
    }

    @Override
    public LinkVO rowInfoLink(Integer id) {
        LinkVO link = friendshipLinkMapper.rowInfoLink(id);
        return link;
    }

    @Override
    public void deleteLink(Integer id) {
        friendshipLinkMapper.deleteLink(id);
    }
}
